import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法十四：使用阻塞队列，这里使用的是有界队列，JDK里阻塞队列很多，但只有ArrayBlockingQueue和LinkedBlockingQueue支持有界队列，使用时需注意
 */
public class ConcurrentTask14 {
    public String str;
    public BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(1);

    public static void main(String[] args) throws InterruptedException {
        ConcurrentTask14 concurrentTask = new ConcurrentTask14();

        new Thread(concurrentTask::putStr).start();
        concurrentTask.blockingQueue.take();
        System.out.println(concurrentTask.getStr());

    }

    public void putStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            blockingQueue.add("ok");
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() {
        return str;
    }
}
